package com.byzk.p2p.admin.city.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.city.dao.CityDao;
import com.byzk.p2p.admin.city.dao.TempCityDao;
import com.byzk.p2p.admin.city.dto.City;
import com.byzk.p2p.admin.city.dto.Province;
import com.byzk.p2p.admin.city.dto.TempCity;

@Service
public class CityService {

	@Autowired
	private CityDao cityDao;
	@Autowired
	private TempCityDao tempCityDao;
	
	/**
	 * 城市列表查询
	 * @param city
	 * @return
	 */
	public List<City> listAll(City city) {
		List<City> cityList = cityDao.list(city);
		return cityList;
	}
	
	/**
	 * 获取城市
	 * @param id 城市id
	 * @return
	 */
	public City getCity(Integer id){
		return cityDao.getCity(id);
	}
	
	/**
	 * 城市列表查询
	 * @param tempCity
	 * @return
	 */
	public List<TempCity> listTempCityAll(TempCity tempCity) {
		return tempCityDao.list(tempCity);
	}
	
	/**
	 * 获取城市
	 * @param id 城市id
	 * @return
	 */
	public TempCity getTempCity(Integer id){
		return tempCityDao.getTempCity(id);
	}
	
	/**
	 * 获取所有的省
	 * @return
	 */
	public List<TempCity> getAllProvinces(){
		return tempCityDao.getAllProvinces();
	}
	
	/**
	 * 获取省下的城市
	 * @param province 省
	 * @return
	 */
	public List<TempCity> getTempCityByProvince(String province){
		return tempCityDao.getTempCityByProvince(province);
	}
	
	public List<TempCity> cityList(){
		TempCity tempCity = new TempCity();
		tempCity.setPid("0");
		return tempCityDao.list(tempCity);
	}
	
	/**
	 * 获取所有的省级城市
	 * @return
	 */
	public List<City> getOneLevelCitys(){
		City oneLevelCity = new City();
		//oneLevelCity.setPid("0001");
		List<City> oneLevelCitys = listAll(oneLevelCity);
		return oneLevelCitys;
	}
	
	
	/**
	 * 获取所有省以及所有省下的市
	 * @return
	 */
	public List<City> getJsonCity(Integer pid) {
		return cityDao.getJsonCity(pid);
	}
	
	/**
	 * 根据城市编码获取城市及其上级城市
	 * @param code
	 * @return
	 */
	public List<City> getCityAndPCityBYCode(String code){
		return cityDao.getCityAndPCityBYCode(code);
	}
	
	/**
	 * 
	* <p>Description:查询城市级联信息 </p>
	* <p>Company:xbd </p>
	* @author:lilei
	* @date:2016年10月19日下午4:07:52
	 */
	public List<City> getCityEnum(City city) {

		List<City> list = new ArrayList<City>();
		List<City> cityList = cityDao.list(city);
		if (city != null && city.getPid()!=null) {
			City province = cityDao.getCity(city.getPid());
			list.add(province);
			if (province.getPid() != -1) {
				City city_ = cityDao.getCity(province.getPid());
				list.add(city_);
			}
		}
		if (list != null) {
			for (City item : cityList) {
				if (item.getPid() == -1 && item.getRoot() == 1) {
					List<City> childList = cityDao.getJsonCity(item.getId());
					List<City> childCity = new ArrayList<City>();
					for (City item_ : childList) {
						City childItem = city.convertCity(item_);
						childCity.add(childItem);
					}
					item.setCity(childCity);
				}
				list.add(item);
			}
		}
		return list;
	}
}
